package com.anjoyo.xgh.musicshare.activity;

import java.util.HashMap;
import java.util.Set;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import org.json.JSONException;
import org.json.JSONObject;

import com.anjoyo.xgh.musicshare.R;
import com.anjoyo.xgh.musicshare.api.UserApi;
import com.anjoyo.xgh.musicshare.entry.User;
import com.anjoyo.xgh.musicshare.util.BitmapUtil;
import com.anjoyo.xgh.musicshare.util.Constant;
import com.anjoyo.xgh.musicshare.util.JsonUtil;
import com.anjoyo.xgh.musicshare.util.MD5Util;
import com.anjoyo.xgh.musicshare.util.MusicSpf;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Contacts;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import cn.sharesdk.framework.AbstractWeibo;
import cn.sharesdk.framework.WeiboActionListener;
import cn.sharesdk.sina.weibo.SinaWeibo;


public class LoginAct extends Activity implements WeiboActionListener{

	EditText etname,etpassword;
	Button btlogin;
	TextView tv_sina_login;
	Context context;
	Intent intent;
	
	Handler handler=new Handler(){
		@Override
		public void handleMessage(Message msg) {
			if (msg.what==Constant.MSG_GET_USERINFO_OK) {
				Toast("获取用户信息成功！");
//				System.out.println((HashMap<String, Object>) msg.obj);
				new UserApi(handler).oAuthRegist((HashMap<String, Object>) msg.obj);
			}
			
			if(msg.what==Constant.MSG_REGIST_OK){
				if(msg.arg1==Constant.MSG_SINA_SUCCESS){
					Toast("新浪注册成功");
					MusicSpf.saveLoginInfo(context, (String) msg.obj);
					JsonUtil jsonUtil = new JsonUtil();
					User user = jsonUtil.paseUser((String) msg.obj);
					new BitmapUtil().saveNetImg(user.getImg_icon(), Constant.CACHE+"head.jpg", handler);
					startActivity(new Intent(context,HomeActivity.class));
				}else if(msg.arg1==Constant.MSG_SINA_ERROR){
					Toast((String) msg.obj);
				}
			}
			
			if (msg.what==Constant.MSG_LOGIN_SUCCESS) {
				Toast("登录成功");
				MusicSpf.saveLoginInfo(context, (String) msg.obj);
				JsonUtil jsonUtil = new JsonUtil();
				User user = jsonUtil.paseUser((String) msg.obj);
				new BitmapUtil().saveNetImg(Constant.SERVER_IP+user.getImg_icon(), Constant.CACHE+"head.jpg", handler);
				startActivity(new Intent(context,HomeActivity.class));
			}else if (msg.what==Constant.MSG_LOGIN_ERROR) {
				Toast((String) msg.obj);
			}else if (msg.what==Constant.DOWLOAD_OK) {
				Toast("头像下载成功");
			}
			super.handleMessage(msg);
		}
	};
	public void Toast(String msg) {
		Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_layout);
		AbstractWeibo.initSDK(this);
		tv_sina_login=(TextView) findViewById(R.id.login_bt_sinalogin);
		etname = (EditText) findViewById(R.id.login_et_name);
		etpassword = (EditText) findViewById(R.id.login_et_password);
		context=this;
		findViewById(R.id.login_bt_login).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				new UserApi(handler).login(etname.getText().toString(), etpassword.getText().toString());
			}
		});
		
		findViewById(R.id.login_tv_regist).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				startActivity(new Intent(context,RegistAct.class));
			}
		});
		
		tv_sina_login.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				
				AbstractWeibo weibo=AbstractWeibo.getWeibo(context, SinaWeibo.NAME);
				if(weibo.isValid()){
					weibo.removeAccount();
				}
				//授权
				weibo.setWeiboActionListener(LoginAct.this);
				//显示当前授权人的信息
				weibo.showUser(null);
			}
		});
		
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// 如果back键被按下的时候
		if (keyCode == KeyEvent.KEYCODE_BACK) {

			// 是否确认退出
			new AlertDialog.Builder(context)
					.setTitle("退出")
					.setMessage("确认退出？")
					.setPositiveButton("确定",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg0,
										int arg1) {
									finish();
								}
							}).create().show();
		}
		return false;
	}
	
	@Override
	public void onCancel(AbstractWeibo arg0, int arg1) {
		
	}
	@Override
	public void onComplete(AbstractWeibo arg0, int arg1,
			HashMap<String, Object> res) {
		Set<String> keys=res.keySet();
//		for (String string : keys) {
//			System.out.println(string+":"+res.get(string));
//		}
		System.out.println("获取新浪用户信息成功！");
		Message message = new Message();
		message.what = Constant.MSG_GET_USERINFO_OK;
		message.obj = res;
		handler.sendMessage(message);
	}
	@Override
	public void onError(AbstractWeibo arg0, int arg1, Throwable arg2) {
	}
}
